import os
import re
import threading

import numpy as np
import time
from commen.log import Logger
import openpyxl
import commen.config
def ImuDatacontinuity():
    #---------读取config文件获取筛选字段内容--------------------------
    key = commen.config.global_config.getRaw('keywords', 'Imubegincontinuation')
    # ---------读取config文件获取筛选字段内容--------------------------
    #-----------------------创建excel--------------------------------------------
    # 获取当前时间
    time_now = time.strftime("%Y%m%d-%H%M", time.localtime())
    # 获取当前路径
    result_root = os.getcwd()
    root = os.getcwd()
    result_path = result_root + r'\result' + "\\" + time_now
    log_path = root + r'\original-logfile' + '\\'
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    for root, dirs, files in os.walk(log_path):
        for file in files:
            aa = os.path.join(root, file)
    # -----遍历对比log路径获取要对比的文件名路径信息----------------------
    book = openpyxl.Workbook()  # 打开一个将写的文件
    sheet = book.create_sheet(index=0)  # 在将写的文件创建sheet
    saveExcel = result_path+"rtx丢包及连续性验证结果.xlsx"
    sheet.cell(1, 2).value = "rtx原值"
    sheet.cell(1, 3).value = "是否丢包"
    sheet.cell(1, 4).value = "是否连续"
    rtklist = []

    row =1
    #-----------------------创建excel--------------------------------------------

    logger  = Logger(logger='TestMylog').getlog()
    fm = open(aa, 'r', encoding='UTF-8') # 打开目标文件
    logger.info("打开目标文件")
    lines = fm.readlines()
    logger.info("获取内容")
    logger.info("开始逐行读取")
    for rtkline in lines:
        if "func rtkDataProc packageData buf" in rtkline:
            bb = rtkline.split("buf :")
            bb = bb[1].rstrip()
            bb = bb.lstrip()
            rtklist.append(bb)

    for i in range(len(rtklist)):
        row =row+1
        logger.info("开始判断第： "+str(row)+"条数据")
        if i+2 > len(rtklist):
            break
        if len(rtklist[i]) != 56:
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "是"
            sheet.cell(row, 4).value = "否"
            continue
        if int(rtklist[i][0:2],16)+4 == int(rtklist[i+1][0:2],16):
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "是"
            continue
        if rtklist[i][0:2] =="FC" and rtklist[i+1][0:2]=="00":
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "是"
            continue
        if rtklist[i][0:2] =="FD" and rtklist[i+1][0:2]=="01":
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "是"
            continue
        if rtklist[i][0:2] =="FE" and rtklist[i+1][0:2]=="02":
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "是"
            continue
        if rtklist[i][0:2] =="FF" and rtklist[i+1][0:2]=="03":
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "是"
            continue
        else:
            sheet.cell(row, 2).value = rtklist[i]
            sheet.cell(row, 3).value = "否"
            sheet.cell(row, 4).value = "否"
    book.save(saveExcel)
ImuDatacontinuity()